package cc.demo02;

import java.lang.reflect.Field;

/**
 * @author ${崔灿}
 * @date 2022/10/27 17:16
 **/

public class Teso02 {
    public static void main(String[] args) throws Exception{
        Class<Teacher> teacherClass = Teacher.class;
        Field[] fields = teacherClass.getFields();
        Teacher teacher = teacherClass.newInstance();
        //获取反射类对应的属性对象
        Field name = teacherClass.getDeclaredField("name");
        //为属性赋值  通过反射默认对指定属性赋值
        name.set(teacher,"74");
        System.out.println(teacher);

        //通过反射获取指定属性的对象值
        Object teacher1 = name.get(teacher);
        System.out.println(teacher1);

        Field age = teacherClass.getDeclaredField("age");
        age.setAccessible(true);
        age.set(teacher,58);
        System.out.println(teacher);

        Object o = age.get(teacher);
        System.out.println(o);

    }
}

class People1{
    private String address;
    private String sex;
}
class Teacher extends People1 {
    public String name;
    private Integer age;

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
